﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once
#include "GnufTree.h"
#include "GnufQtItem.h"
#include "GnufQtTreeItemDelegate.h"
#include "GnufCustomizedDelegate.h"
#include <QtWidgets/QTreeWidget>
#include <QPoint>

class QKeySequence;

namespace gnuf {
    class TreeViewAppearanceSetting;
    // 效率太低，要优化
    // 考虑替换成QStandItemModel + QTreeView的View/Model方式
    // 暂不支持列表头点击排序，因为开发层难以介入QTreeWidget的排序过程
    class QtSortFilterProxyModel;
    class QtTreeBoard 
        : public QtItemWith<QTreeWidget, TreeBoard>
        , public ICustomizedDelegateHost
    {
        Q_OBJECT
    public:
        QtTreeBoard(TreeBoard *);
        virtual ~QtTreeBoard();
        virtual void refresh(QtItem::EWhatDirty) override;

    public:
        bool hasIconClicked(QTreeWidgetItem* item) const;
        void takeClickedIconColumn(QTreeWidgetItem* item, std::list<int>& clickedColumn, std::list<int>& unClickedColumn) const;
        void addTree(TreeNode* node);
        void insertTreeBefore(TreeNode* node, TreeNode* nodeInsert);
        QModelIndex indexFromNode(const TreeNode* node) const;
        const TreeNode* nodeFromIndex(const QModelIndex& index) const;
        void deleteTree(TreeNode* node);
        void filterRowByText(const std::wstring& text);
        void refreshIndex();
        void refreshGeometries();

        bool setCustomizedDelegate(TreeNode *, CustomizedDelegateBase *, const bool wantToTransferOwnership);

    public:
        void setHasLinkLine(bool bHas);
        inline QtTreeItemDelegate* treeItemDelegate() { return m_itemDelegate; }

    private:
        void setStyle();
        const TreeViewAppearanceSetting* appearanceSetting();

    private:
        virtual void mouseReleaseEvent(QMouseEvent *) override;
        virtual void keyPressEvent(QKeyEvent *) override;
        virtual void dragMoveEvent(QDragMoveEvent *) override;
        virtual void mouseMoveEvent(QMouseEvent* event) override;
        virtual void leaveEvent(QEvent *) override;

        // Inherits from ICustomizedDelegateHost...
        virtual const Item * itemAtModelIndex(const QModelIndex &) const override;
        virtual bool modelIndexOfItem(const Item *, QModelIndex &) const override;

    public:
    Q_SIGNALS:
        void onItemEdited(QLineEdit* pLineEdit, const QKeySequence& keySequence);
        void onCreateEditControl(QLineEdit* pLineEdit);

    private Q_SLOTS:
        void onItemClicked(QTreeWidgetItem *item, int column);
        void onItemChanged(QTreeWidgetItem *item, int column);
        void onItemDoubleClicked(QTreeWidgetItem *item, int column);
        void onFilterIndex(const QModelIndex& index, bool bFilter) const;
        void onItemPressed(QTreeWidgetItem *item, int column);
        void onSelectionChanged();

    private:
        QModelIndex m_selectedModelIndex;
        QtTreeItemDelegate* m_itemDelegate;
        QtSortFilterProxyModel* m_proxyModel;
        gcmp::WeakPtr<QtTreeItem> m_curPressItem;
        QPoint m_dragStartPosition;
    };
}
